const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost/";

var fn1 = function (myobj) {
    MongoClient.connect(url, function (err, db) {
        if (err) throw err;
        console.log('数据库已创建');
        var dbase = db.db("runoob");
        // var myobj = { name: "菜鸟教程", url: "www.runoob" };
        // return  dbase.collection("test02")
        dbase.collection("test02").insertOne(myobj, function(err, res) {
            if (err) throw err;
            console.log("文档插入成功");
            db.close();
        });
        // dbase.createCollection('test01', function (err, res) {
        //     if (err) throw err;
        //     console.log("创建集合!");
        //     db.close();
        // });
    });
}
var fn2 = function () {
    return new Promise((res,reject) => {
        MongoClient.connect(url, function (err, db) {
            if (err) throw err;
            console.log('数据库已创建');
            var dbase = db.db("runoob");
            var myobj = { name: "菜鸟教程", url: "www.runoob" };
            // return  dbase.collection("test02")
            dbase.collection("test02"). find({}).toArray(function(err, result) { // 返回集合中所有数据
                if (err) throw err;
                // return result;
                res(result)
                console.log(result);
                db.close();
            });
    })
        // dbase.createCollection('test01', function (err, res) {
        //     if (err) throw err;
        //     console.log("创建集合!");
        //     db.close();
        // });
    });
}

var fn3 = function () {
    return new Promise((res,reject) => {
        MongoClient.connect(url, function (err, db) {
            if (err) throw err;
            console.log('数据库已创建');
            var dbase = db.db("runoob");
            var myobj = { name: "菜鸟教程", url: "www.runoob" };
            // return  dbase.collection("test02")
            dbase.collection("test02").insertOne(myobj, function(err, res) {
                if (err) throw err;
                res()
                console.log("文档插入成功");
                db.close();
            });
    })
    
        // dbase.createCollection('test01', function (err, res) {
        //     if (err) throw err;
        //     console.log("创建集合!");
        //     db.close();
        // });
    });
}
// --------------------------------

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});
// -------------------------------------
app.get('/add',(req,res) => {
    // res.send('nihao')
    console.log(req.query);
    fn1(req.query)
})
app.get('/select',(req,res) => {
    fn2().then((data) => {
        res.send(data)
    })
})
app.get('/delete',(req,res) => {
    fn1()
})
app.listen(10000)
console.log('服务器启动成功');